package com.example.wengwyue.patientapplication.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wengwyue.patientapplication.R;
import com.example.wengwyue.patientapplication.network.ApiManager;
import com.hospital.common.api.DataCallback;
import com.hospital.common.api.model.Error;

/**
 * 作者： wengwyue   时间： 16/8/10.
 * 功能：
 * 修改：
 */
public class PayFragment extends BaseVaryViewFragment {
    private EditText inputMoney;
    private TextView confirmPay;
    private View rootView;
    private RadioGroup group;
    @Override
    protected void initView(View rootView) {
        inputMoney = (EditText) rootView.findViewById(R.id.edit_input_money);
        confirmPay = (TextView) rootView.findViewById(R.id.comfirm_pay);
        group = (RadioGroup) rootView.findViewById(R.id.radiogroup_layout);
        this.rootView = rootView;
        super.initView(rootView);
    }

    @Override
    public View provideDataView() {
        return rootView;
    }

    @Override
    protected void initListener() {
        inputMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    confirmPay.setText(String.format(getString(R.string.comfirm_pay_text_edit),s.toString()));
                } else {
                    confirmPay.setText(getString(R.string.comfirm_pay_text_null));

                }
                confirmPay.setTag(s.toString());
            }
        });

        confirmPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                putBalance(Double.valueOf((String) v.getTag()));
            }
        });

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                if (checkedId == ) {
//
//                }
            }
        });
    }

    @Override
    protected void initData(View rootView, Bundle savedInstanceState) {

    }

    @Override
    protected int provideLayoutResId() {
        return R.layout.fragment_pay;
    }

    private void putBalance(double money) {
        mVaryViewHelper.showLoadingView();
        ApiManager.putBalance(money, new DataCallback() {
            @Override
            public void onSuccess(Object data) {
                mVaryViewHelper.showDataView();
                if (getActivity() != null) {
                    Toast.makeText(getActivity(),"充值成功！",Toast.LENGTH_LONG).show();
                    inputMoney.setText("");
                }
            }

            @Override
            public void onFailed(Error error) {
                mVaryViewHelper.hideLoadingView();
                if (getActivity() != null) {
                    Toast.makeText(getActivity(),"充值失败！",Toast.LENGTH_LONG).show();
                }
            }

        });
    }
}
